<template>
	<div>
		<!-- 面包屑导航 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>普通用户管理</el-breadcrumb-item>
			<el-breadcrumb-item>用户列表</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 卡片区 -->
		<el-card>
			<!-- 表格上部搜索，添加用户区 -->
			<el-row :gutter="20">
				<el-col :span="6">
					<el-input placeholder="请输入内容" v-model="searchStr">
						<el-button slot="append" icon="el-icon-search" v-on:click="search(searchStr)"></el-button>
					</el-input>
				</el-col>
				<el-col :span="6">
					<el-button type="primary" v-on:click="addDialogVisible=true">添加用户</el-button>
				</el-col>
			</el-row>
			<!-- 表格 -->
			<el-table :data="customerList" style="width: 100%" border stripe>
				<el-table-column type="index" label="#"></el-table-column>
				<el-table-column prop="customer_id" label="用户ID"></el-table-column>
				<el-table-column prop="customer_email" label="用户邮箱"></el-table-column>
				<el-table-column prop="customer_name" label="用户名"></el-table-column>
				<el-table-column prop="customer_type" label="用户类型">
				</el-table-column>
				<el-table-column prop="customer_limit" label="签约限额(元)"></el-table-column>
				<el-table-column prop="customer_used_limit" label="使用额度(元)"></el-table-column>
				<el-table-column label="操作">
					<template slot-scope="scope">
						<el-button type="primary" icon="el-icon-edit" size="mini" @click="openEditDialog(scope.row.customer_id)"></el-button>
						<el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteById(scope.row.customer_id)"></el-button>					
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页区 -->
			<div class="block">
				<el-pagination @current-change="handleCurrentChange" :current-page.sync="pagenum" :page-size="5" layout="prev, pager, next, jumper, total"
				 :total="customernum">
				</el-pagination>
			</div>
		</el-card>
		<!-- 添加用户的弹出对话框 -->
		<el-dialog title="添加用户" :visible.sync="addDialogVisible" width="40%" @close="addFormClose()">
			<!-- 主体区 -->
			<el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
				<el-form-item label="用户名" prop="customer_name">
					<el-input v-model="addForm.customer_name"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="customer_email">
					<el-input v-model="addForm.customer_email"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="customer_password">
					<el-input v-model="addForm.customer_password" show-password></el-input>
				</el-form-item>
				<el-form-item label="用户类型" prop="customer_type">
					<el-select v-model="addForm.customer_type" placeholder="请选择用户类型">
						<el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<span style="font-style:oblique; color: red; ">* 签约型用户，月末结算总金额</span>
				</el-form-item>
				<el-form-item label="用户限额" prop="customer_limit">
					<el-input v-if="addForm.customer_type=='现金'" v-model="addForm.customer_limit=0"  :disabled="true"></el-input>
					<el-input v-else v-model="addForm.customer_limit"></el-input>
				</el-form-item>
			</el-form>
			<!-- 底部区域 -->
			<span slot="footer" class="dialog-footer">
				<el-button @click="addDialogVisible = false">取消</el-button>
				<el-button type="primary" @click="addCustomer()">确定</el-button>
			</span>
		</el-dialog>
		<!-- 编辑用户的弹出对话框 -->
		<el-dialog title="编辑用户" :visible.sync="editDialogVisible" width="40%">
			<!-- 主体区 -->
			<el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
				<el-form-item label="用户名" prop="customer_name">
					<el-input v-model="editForm.customer_name" disable></el-input>
				</el-form-item>
				<el-form-item label="用户邮箱" prop="customer_email">
					<el-input v-model="editForm.customer_email"></el-input>
				</el-form-item>
				<el-form-item label="用户密码" prop="customer_password">
					<el-input v-model="editForm.customer_password" show-password></el-input>
				</el-form-item>
				<el-form-item label="用户类型" prop="customer_type">
					<el-select v-model="editForm.customer_type" placeholder="请选择用户类型">
						<el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="用户限额" prop="customer_limit">
					<el-input v-if="editForm.customer_type=='现金'" v-model="editForm.customer_limit=0"  :disabled="true"></el-input>
					<el-input v-else v-model="editForm.customer_limit"></el-input>
				</el-form-item>
	
			</el-form>
			<!-- 底部区域 -->
			<span slot="footer" class="dialog-footer">
				<el-button @click="addDialogVisible = false">取消</el-button>
				<el-button type="primary" @click="editCustomer()">确定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		created() {
			this.getCustomerList(1, '')

		},
		data() {
			return {
				searchStr: '',
				customerList: [],
				//当前所在页码
				pagenum: 0,
				//数据共多少页
				totalpage: 0,
				//数据总条数
				customernum: 0,
				//控制用户添加对话框的可见与否
				addDialogVisible: false,
				//添加新用户表单
				addForm: {
					customer_name: '',
					customer_email: '',
					customer_password: '',
					customer_type: '',
					customer_limit: ''
				},
				//添加新用户的表单规则
				addFormRules: {
					customer_name: [{
							required: true,
							message: "请输入用户名",
							trigger: "blur"
						},
						{
							min: 3,
							max: 10,
							message: "用户名长度在3-10之间",
							trigger: "blur"
						}
					],
					customer_email: [{
							required: true,
							message: "请输入邮箱",
							trigger: "blur"
						},
						{
							type: "email",
							required: true,
							message: "邮箱格式错误",
							trigger: "blur"
						},
					],
					customer_password: [{
						required: true,
						message: "请输入密码",
						trigger: "blur"
					}],
					customer_type: [{
							required: true,
							message: "请输入用户类型",
							trigger: "blur"
					}]
				},

				//编辑用户弹出框的可见与否
				editDialogVisible: false,
				//编辑表单
				editForm: {
					customer_name: '',
					customer_email: '',
					customer_password: '',
					customer_type: 1,
					customer_limit: 0
				},
				//编辑用户的表单规则和新增的表单规则相同
				editFormRules: {
					customer_name: [{
							required: true,
							message: "请输入用户名",
							trigger: "blur"
						},
						{
							min: 3,
							max: 10,
							message: "用户名长度在3-10之间",
							trigger: "blur"
						}
					],
					customer_email: [{
							required: true,
							message: "请输入邮箱",
							trigger: "blur"
						},
						{
							type: "email",
							required: true,
							message: "邮箱格式错误",
							trigger: "blur"
						},
					],
					customer_password: [{
						required: true,
						message: "请输入密码",
						trigger: "blur"
					}]
				},
				//编辑的用户的id
				editCustomerId: 0,
				//用户类型的选择框
				typeOptions: [{
					value: '签约',
					label: '签约'
				}, {
					value: '现金',
					label: '现金'
				}]

			}
		},
		methods: {
			//拿到表格展示的所有用户数据数据
			getCustomerList(pagenum, searchStr) {
				this.$axios
					.get("/customer?pagenum=" + pagenum + "&searchStr=" + searchStr)
					.then(backinfo => {
						console.log(backinfo)
						//要用的数据在backinfo.data中,把这个值给res。该写法是ES6解构对象。
						const {
							data: res
						} = backinfo
						if (res.err_code == 0) {
							this.customerList = res.data.customers
							this.pagenum = res.data.pagenum
							this.totalpage = res.data.totalpage
							this.customernum = res.data.customernum
							//return this.$message.success(res.message)
						} else {
							this.customerList = []
							this.pagenum = 1
							this.totalpage = 1
							this.customernum = 0
						}
					})
			},
			//页面改变调用,自带新页的页码做参数
			handleCurrentChange(newpagenum) {
				this.getCustomerList(newpagenum, this.searchStr)
			},
			//搜索
			search(searchStr) {
				this.getCustomerList(1, searchStr)
			},
			//添加用户
			addCustomer() {
				this.$refs.addFormRef.validate(valid => {
					if (!valid) { //格式验证不对，res为flase
						return
					}
					console.log("用户添加表单验证成功")
					var res = this.$axios
						.post('/customer', this.addForm)
						.then((backinfo) => {
							//	console.log(backinfo)	
							const {
								data: res
							} = backinfo
							if (res.err_code == 1) {
								//添加用户失败
								this.$message.error(res.message)
							} else {
								//添加用户成功
								this.$message.success(res.message)
								this.addDialogVisible = false
								this.getCustomerList(1, '')
							}
						})
				})

			},
			//关闭对话框（即可见性为false）触发，清空填入数据
			addFormClose() {
				this.$refs.addFormRef.resetFields()
			},
			//删除数据
			deleteById(customer_id) {
				this.$axios
					.delete("/customer?customer_id=" + customer_id)
					.then(backinfo => {
						//要用的数据在backinfo.data中,把这个值给res。该写法是ES6解构对象。
						const {
							data: res
						} = backinfo
						if (res.err_code == 0) {
							this.$message.success(res.message)
							return this.getCustomerList(1, '')
						} else {
							return this.$message.error(res.message)


						}
					})

			},
			//打开编辑数据对话框,把要编辑用户的数据带进去
			openEditDialog(customer_id) {
				//console.log(customer_id)
				this.editCustomerId = customer_id
				this.editDialogVisible = true
				this.$axios
					.get("customer/" + customer_id)
					.then(backinfo => {
						const {
							data: res
						} = backinfo
						//console.log(res)
						if (res.err_code == 0) {
							this.editForm.customer_name = res.data.customer_name
							this.editForm.customer_email = res.data.customer_email
							this.editForm.customer_password = res.data.customer_password
							this.editForm.customer_type = res.data.customer_type
							this.editForm.customer_limit = res.data.customer_limit
						} else {
							return this.$message.error(res.message)


						}

					})


			},
			//编辑对话框的确定按钮触发保存
			editCustomer() {
				this.$refs.editFormRef.validate(valid => {
					if (!valid) { //格式验证不对，res为flase
						return
					}
					console.log("用户编辑表单验证成功")
					var res = this.$axios
						.put('/customer/' + this.editCustomerId, this.editForm)
						.then((backinfo) => {
							//	console.log(backinfo)	
							const {
								data: res
							} = backinfo
							if (res.err_code == 1) {
								//编辑用户失败
								this.$message.error(res.message)
							} else {
								//编辑用户成功
								this.$message.success(res.message)
								this.editDialogVisible = false
								this.getCustomerList(1, '')
							}
						})
				})

			}



		}

	}
</script>

<style lang="less" scoped>

</style>
